﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Data.Objects.SqlClient;

namespace University.Workshop.DataAccess
{
    public class DataAccessExpense
    {
        public static void CreateExpense(Model.Expense newExpense)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.Spend.Add(newExpense);
            WFPEntities.Instance.SaveChanges();
        }

        public static void UpdateExpense(Model.Expense modifyExpense)
        {
            WFPEntities.Instance.DiscardChanges();
            Model.Expense q = (from wp in WFPEntities.Instance.Spend
                               where wp.DateRegistry == modifyExpense.DateRegistry
                               select wp).First();

            q.UserId = modifyExpense.UserId;
            q.SpendDate = modifyExpense.SpendDate;
            q.Price = modifyExpense.Price;
            q.ExpenseType = modifyExpense.ExpenseType;
            q.Amount = modifyExpense.Amount;
            q.Description = modifyExpense.Description;
            q.Receipt = modifyExpense.Receipt;

            WFPEntities.Instance.SaveChanges();
        }

        public static ICollection<Model.Expense> SearchExpenseByParameterAll(Model.Expense waterPurchaseToFind)
        {
            if (waterPurchaseToFind.ExpenseType == 10)
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from w in WFPEntities.Instance.Spend
                        where (w.Receipt.Contains(waterPurchaseToFind.Receipt) &&
                              w.SpendDate.Equals(waterPurchaseToFind.SpendDate))
                        select w;
                return q.ToList();
            }
            else
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from w in WFPEntities.Instance.Spend
                        where (w.Receipt.Contains(waterPurchaseToFind.Receipt) &&
                              w.SpendDate.Equals(waterPurchaseToFind.SpendDate) &&
                              w.ExpenseType.Equals(waterPurchaseToFind.ExpenseType)
                              )
                        select w;
                return q.ToList();
            }

        }

        public static ICollection<Model.Expense> SearchExpenseMinusDatePurchase(Model.Expense waterPurchaseToFind)
        {
            if (waterPurchaseToFind.ExpenseType == 10)
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from w in WFPEntities.Instance.Spend
                        where (w.Receipt.Contains(waterPurchaseToFind.Receipt))
                        select w;
                return q.ToList();
            }
            else
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from w in WFPEntities.Instance.Spend
                        where (w.Receipt.Contains(waterPurchaseToFind.Receipt) &&
                            w.ExpenseType.Equals(waterPurchaseToFind.ExpenseType)
                        )
                        select w;
                return q.ToList();
            }
        }

        public static ICollection<Expense> GetExpenseByReceipt(Model.Expense waterPurchase)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from u in WFPEntities.Instance.Spend
                    where u.Receipt == waterPurchase.Receipt
                    select u).ToList();
        }

        public static ICollection<Model.Expense> GetExpenseById(DateTime date)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from u in WFPEntities.Instance.Spend
                    where u.DateRegistry == date
                    select u).ToList();
        }

        public static ICollection<ExpenseControl> GetExpenseBetweenDates(DateTime initialDate, DateTime endDate)
        {
            WFPEntities.Instance.DiscardChanges();
            List<ExpenseControl> expenseList = new List<ExpenseControl>();
            var list = (from u in WFPEntities.Instance.Spend
                        where u.DateRegistry >= initialDate && u.DateRegistry <= endDate
                        select new Model.ExpenseControl
                        {
                            DateRegistry = u.DateRegistry,
                            SpendDate = u.SpendDate,
                            ExpenseType = u.ExpenseType,
                            StringExpenseType = SqlFunctions.StringConvert((decimal)(short)u.ExpenseType),
                            expensePaymentAmount = u.Price,
                            UserId = u.UserId,
                            Amount = u.Amount,
                            IsWaterExpense = false
                        }).ToList();

            expenseList.AddRange(list);
            return expenseList;
        }
    }
}
